package com.leave.dao;

import com.leave.entity.Classes;
import com.leave.entity.Course;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseDao {
    private JdbcTemplate jdbcTemplate;

        public List<Course> query(Course course) {
            String sql = "select * from sys_course where 1=1 ";
            List<Object> paramValues = new ArrayList<>();
            List<Course> courseList = new ArrayList<>();
            if(course != null) {
                if (course.getCourseID() != null && !"".equals(StringUtils.trim(course.getCourseID()))) {
                    sql += " and courseID=?";
                    paramValues.add(course.getCourseID());
                }
            }
            courseList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Course>() {
                @Override
                public Course mapRow(ResultSet rs, int rowNum) throws SQLException {
                   Course course1 = new Course();
                    String courseID = rs.getString("courseID");
                    String classID = rs.getString("classID");
                    String courseName = rs.getString("courseName");
                    String year = rs.getString("year");
                    String term = rs.getString("term");
                    int hour = rs.getInt("hour");
                    course1.setCourseID(courseID);
                    course1.setCourseName(courseName);
                    course1.setClassID(classID);
                    course1.setTerm(term);
                    course1.setHour(hour);
                    course1.setYear(year);
                    return course1;
                }
            });
            return courseList;
        }

    public void delete(String courseID){
        String sql = "delete from sys_course where courseID=?";
        String[]paramValues = {courseID};
        jdbcTemplate.update(sql,paramValues);
    }

    public void insert(Course course) {
        //声明插入SQL字符串变量sql
        String sql = "insert into sys_course(courseID,classID,courseName,year,term,hour)values(?,?,?,?,?,?)";
        if (course != null) {
            String[] paramValues = {course.getCourseID(), course.getClassID(), course.getCourseName(), course.getYear(), course.getTerm(), String.valueOf(course.getHour())};
            jdbcTemplate.update(sql, paramValues);
        }
    }

    public void update(Course course) {
        // 声明更新 SQL 字符串变量 sql
        String sql = "update sys_course set courseID=?,classID=?,courseName=?,year=?,term=?,hour=? where courseID=?";
        if (course != null) {
            String[] paramValues = {course.getCourseID(), course.getClassID(), course.getCourseName(), course.getYear(), course.getTerm(), String.valueOf(course.getHour()), course.getCourseID()};
            jdbcTemplate.update(sql, paramValues);
        }
    }

    public List<Map<String, Object>> selectCourseList(String keyword) throws SQLException {
        String sql = "SELECT * FROM sys_course WHERE courseID LIKE ? OR classID LIKE ? OR courseName LIKE ? OR year LIKE ? OR term LIKE ? OR hour LIKE ?";
        String likeKeyword = "%" + keyword + "%";
        String[] paramValues = {likeKeyword, likeKeyword, likeKeyword, likeKeyword, likeKeyword, likeKeyword};
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String, Object>>() {
            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map<String, Object> map = new HashMap<>();
                int columnCount = rs.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    map.put(columnName, columnValue);
                }
                return map;
            }
        };
        return jdbcTemplate.query(sql, paramValues, rowMapper);
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

}
